<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post_Cate extends AdminBackEndController {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/post_cate_model', 'post_cate_model');
    }

    public function index()
    {
        $aPostCate = $this->post_cate_model->getAllPostCate(true);
        $aReturn = array('pageTitle' => 'Danh mục tin tức', 'aPostCate' => $aPostCate);
        $this->renderView('backend/admin/post/cate', $aReturn);
    }

    public function insert(){
        $oPostCate = new Post_Cate_Entity();
        $aReturn = array(
            'pageTitle'  => 'Thêm mới | Danh mục | Tin tức',
            'title' => 'Thêm mới',
            'case' => 'insert',
            'action' => admin_post_url(CATEGORY_SLUG.'/'.INSERT_SLUG.'/do'),
            'oPostCate' => $oPostCate
        );
        $this->renderView('backend/admin/post/cate-single', $aReturn);
    }

    public function update($slug){
        $oPostCate = $this->post_cate_model->getPostCate(array('slug'=>$slug));
        if(count($oPostCate)!=1){ show_404(); } else { $oPostCate = $oPostCate[0]; }
        $aReturn = array(
            'pageTitle'  => 'Cập nhật | Danh mục | Tin tức',
            'title' => 'Cập nhật',
            'case' => 'update',
            'action' => admin_post_url(CATEGORY_SLUG.'/'.UPDATE_SLUG.'/do'),
            'oPostCate' => $oPostCate
        );
        $this->renderView('backend/admin/post/cate-single', $aReturn);
    }

    private function setValidate(){
        $this->form_validation->set_rules('post_cate[title]', 'lang:post_cate_title', 'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('post_cate[slug]',  'lang:post_cate_slug',  'trim|required|callback_slug_check|xss_clean');
        $this->form_validation->set_rules('post_cate[order]', 'lang:post_cate_order', 'trim|required|integer|max_length[10]|xss_clean');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
    }

    public function doInsert(){
        $aResult = array();
        $postCate = $this->input->post('post_cate', true);
        $this->setValidate();
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('insert_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
                );
        } else {
            $isExsist =$this->post_cate_model->checkPostCateExist(0, $postCate);
            if($isExsist > 0){
                $aResult = array(
                    'code' => 0,
                    'title'=> $this->lang->line('insert_fail'),
                    'text' => '<ul><li>'.$this->lang->line('post_cate_exist').'</li></ul>'
                );
            } else {
                $postCate['created_at'] = get_current_date();
                $result = $this->post_cate_model->insert($postCate);
                if($result){
                    $aResult = array(
                        'code' => 1,
                        'text'=> $this->lang->line('insert_success'),
                        'href' => admin_post_url(CATEGORY_SLUG . '/' . trim($postCate['slug']))
                    );
                } else {
                    $aResult = array(
                        'code' => 0,
                        'title' => $this->lang->line('system_error')
                    );
                }
            }
        }
        $this->renderJSON($aResult);
    }

    public function doUpdate(){
        $curPostCateId = 0;
        $isError       = false;
        $aResult       = array();
        $postCate      = $this->input->post('post_cate', true);
        $tPostCate     = $this->input->post('post_cate');

        //validate data
        if(isset($postCate['id'])){
            $curPostCateId = $this->encrypt->decode($tPostCate['id']);
            if($curPostCateId==false){
                $isError = true;
            }
        } else {
            $isError = true;
        }
        if(count($aResult)>0){
            $aResult = array(
                'code'=>0,
                'title' => $this->lang->line('system_error')
            );
            $this->renderJSON($aResult);
            return;
        }

        //update
        if(isset($postCate['case'])){
            if($postCate['case'] == UPDATE_FULL){
                $isExsist = $this->post_cate_model->checkPostCateExist($curPostCateId, $postCate);
                if($isExsist > 0){
                    $aResult = array(
                        'code' => 0,
                        'title' => $this->lang->line('system_error')
                    );
                } else {
                    unset($postCate['case']);
                    $aResult = $this->fullUpdate($curPostCateId, $postCate);
                }
            } else {
                $aResult = $this->flashUpdate($curPostCateId, $postCate);
            }
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
            );
        }

        $this->renderJSON($aResult);
    }

    private function flashUpdate($curPostCateId, $postCate){
        $date      = get_current_date();
        $aResult   = array();
        $tPostCate = array();
        if(isset($postCate['delete_flg']) && $postCate['delete_flg']==1){
            $tPostCate['deleted_at'] = NULL;
            $tPostCate['updated_at'] = $date;
        } else {

            $tPostCate['deleted_at'] = $date;
        }
        $result = $this->post_cate_model->update($curPostCateId, $tPostCate);
        if($result){
            $aResult = array(
                'code' => 1,
                'text'=> $this->lang->line('update_success')
            );
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
            );
        }
        return $aResult;
    }

    private function fullUpdate($curPostCateId, $postCate){
        $aResult = array();
        $date    = get_current_date();
        $this->setValidate();
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('update_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
                );
        } else {
            $isExsist =$this->post_cate_model->checkPostCateExist($curPostCateId, $postCate);
            if($isExsist > 0){
                $aResult = array(
                    'code' => 0,
                    'title'=> $this->lang->line('system_error')
                );
            } else {
                if(isset($postCate['delete_flg']) && $postCate['delete_flg']==1){
                    $postCate['deleted_at'] = NULL;
                    $postCate['updated_at'] = $date;
                } else {
                    $postCate['deleted_at'] = $date;
                }
                unset($postCate['id']);
                unset($postCate['delete_flg']);

                $result = $this->post_cate_model->update($curPostCateId, $postCate);
                if($result){
                    $aResult = array(
                        'code'     => 1,
                        'text'     => $this->lang->line('update_success'),
                        'cmd_case' => 'update',
                        'href'     => admin_post_cate_url(trim($postCate['slug'])),
                        'front_href'     => post_cate_url(trim($postCate['slug']))
                        );
                } else {
                    $aResult = array(
                        'code' => 0,
                        'title' => $this->lang->line('system_error')
                    );
                }
            }
        }
        return $aResult;
    }
}
?>